package services

import (
	"errors"
	"fmt"
	database "hft/Database"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	"hft/Http/Models/request"
	utils "hft/Http/Utils"
	"hft/global"
	"hft/tools"
)

type LouPanName struct{}

func (*LouPanName) Create(data *admin.LouPanName) (err error) {

	var region *admin.Region

	err = global.OC_DB.First(&region, data.RegionId).Error

	if err != nil {
		return errors.New("找不到该地区")
	}
	data.RegionLink = fmt.Sprintf("%v%v,", region.Link, region.ID)
	data.BelongName = utils.AccountBelongRegion(data.RegionLink, global.OC_DB)
	data.HasUse = 1

	res := global.OC_DB.Where("name = ? AND region_id = ?", data.Name, data.RegionId).FirstOrCreate(&data)

	if res.Error != nil {
		return errors.New("未知错误500")
	}

	if res.RowsAffected == 0 {
		return errors.New("该楼盘在所在城市已存在")
	}

	return nil

}

func (*LouPanName) Edit(data *admin.LouPanName) (err error) {

	var region *admin.Region

	err = global.OC_DB.First(&region, data.RegionId).Error

	if err != nil {
		return errors.New("找不到该地区")
	}
	data.RegionLink = fmt.Sprintf("%v%v,", region.Link, region.ID)
	data.BelongName = utils.AccountBelongRegion(data.RegionLink, global.OC_DB)
	if data.HasUse != 1 && data.HasUse != 2 {
		data.HasUse = 1
	}

	// var louName *admin.LouPanName

	// err = global.OC_DB.First(&louName, data.ID).Error

	// if err != nil {
	// 	return errors.New("找不到该楼盘名字")
	// }

	// var lou *admin.LouPan

	// err = global.OC_DB.Where("name = ? AND region = ?", louName.Name, louName.RegionId).First(&lou).Error

	// if err != nil {
	// 	return errors.New("该楼盘不存在")
	// }

	err = global.OC_DB.Model(&admin.LouPanName{}).Where("id = ?", data.ID).Updates(&data).Error

	if err != nil {
		return errors.New("该楼盘在所在城市已存在")
	}

	// err = global.OC_DB.Model(&admin.LouPan{}).Where("id = ?", lou.ID).Updates(map[string]interface{}{
	// 	"name":        data.Name,
	// 	"region":      data.RegionId,
	// 	"region_link": data.RegionLink,
	// 	"belong_name": data.BelongName,
	// }).Error

	// if err != nil {
	// 	return errors.New("未知错误500")
	// }

	return nil

}

func (*LouPanName) List(paginate *request.LouPanNamePage) (page *utils.Page, err error) {

	var list []*admin.LouPanName

	var region *admin.Region

	err = global.OC_DB.Find(&region, paginate.RegionId).Error

	if err != nil {
		return nil, errors.New("该地区不存在")
	}

	paginate.RegionLink = fmt.Sprintf("%v%v,", region.Link, region.ID)

	err = global.OC_DB.Scopes(dao.LouPanNameDao.GetLouPanNameList(paginate, global.OC_DB)).Find(&list).Scopes(dao.GetCount(global.OC_DB)).Count(&paginate.Total).Error

	if err != nil {
		return nil, errors.New("未知错误500")
	}

	paginate.Data = list

	return &paginate.Page, nil
}

func (*LouPanName) Delete(ids []int) (err error) {

	var list []*admin.LouPanName

	for _, v := range ids {
		var item = &admin.LouPanName{Base: database.Base{ID: uint(v)}}
		list = append(list, item)
	}

	global.OC_DB.Unscoped().Delete(&list)

	// var lous []*admin.LouPan

	// var names []string

	// for _, v := range list {
	// 	names = append(names, v.Name)
	// }

	// global.OC_DB.Where("name IN ?", names).Find(&lous)

	// global.OC_DB.Unscoped().Delete(&lous)

	return nil

}

func (*LouPanName) GetLouNameByAccount(paginate *request.LouNameAccountPage, claims tools.BaseClaims) (page *utils.Page, err error) {

	var account *admin.Account

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("找不到该账号信息")
	}

	var louPanNameList []*admin.LouPanName

	global.OC_DB.Where("region_link LIKE ?", fmt.Sprintf("%v%%", account.RegionLink)).Find(&louPanNameList)

	list, total := dao.LouPanNameDao.SearchLouPanNameList(paginate, louPanNameList)

	paginate.Data = list
	paginate.Total = total

	return &paginate.Page, nil
}

var LouPanNameServices = new(LouPanName)
